package com.arch.dev.client.view.ui;


import java.util.List;
import java.util.Set;

import com.arch.dev.client.vo.PdfVO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SelectionModel;

public class PdfTable extends Composite {

	private static BudgetTableUiBinder uiBinder = GWT

	.create(BudgetTableUiBinder.class);

	interface BudgetTableUiBinder extends UiBinder<Widget, PdfTable> {

	}

	@UiField(provided = true)
	CellTable<PdfVO> cellTable;

	@UiField(provided = true)
	BudgetToolBar budgetToolBar;

	@UiField(provided = true)
	SimplePager pager;

	Widget view;

	ListDataProvider<PdfVO> dataProvider;

	public PdfTable() {

		onInitialize();

		initWidget(uiBinder.createAndBindUi(this));

	}

	public PdfTable(Widget view) {

		this();

		this.view = view;

	}

	SelectionModel<PdfVO> selectionModel;

	
	
	public void onInitialize() {	
		

		BudgetImageBundle bundle = (BudgetImageBundle) GWT

				.create(BudgetImageBundle.class);

		budgetToolBar = new BudgetToolBar();

		budgetToolBar.removeAllWidgets();
		
		
		cellTable = new CellTable<PdfVO>();

		cellTable.setWidth("100%", true);

		// Add a selection model so we can select cells.

		selectionModel = new MultiSelectionModel<PdfVO>(

		PdfVO.KEY_PROVIDER);

		cellTable

		.setSelectionModel(selectionModel, DefaultSelectionEventManager

		.<PdfVO> createCheckboxManager());

		// Create a Pager to control the table.

		SimplePager.Resources pagerResources = GWT

		.create(SimplePager.Resources.class);

		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,

		true);

		pager.setDisplay(cellTable);

		// Initialize the columns.

		initTableColumns();

		cellTable.setPageSize(10);

		dataProvider = new ListDataProvider<PdfVO>();

		// Connect the table to the data provider.

		dataProvider.addDataDisplay(cellTable);

	}

	/**
	 * 
	 * Add the columns to the table.
	 */

	private void initTableColumns() {

		// Create name column.

		TextColumn<PdfVO> nameColumn = new TextColumn<PdfVO>() {

			@Override
			public String getValue(PdfVO contact) {

				return contact.getName();

			}

		};

		// Make the name column sortable.

		nameColumn.setSortable(true);

		final DateTimeFormat dateFormat = DateTimeFormat

		.getFormat(PredefinedFormat.DATE_FULL);

		// Create address column.

		TextColumn<PdfVO> dateColumn = new TextColumn<PdfVO>() {

			@Override
			public String getValue(PdfVO contact) {

				return dateFormat.format(contact.getDate());

			}

		};

		Column<PdfVO, String> linkColumn = new Column<PdfVO, String>(

		new LinkCell()) {

			public String getValue(PdfVO p) {

				return p.getUrl();

			}

		};

		/*
		 * 
		 * // Create name column.
		 * 
		 * TextColumn<PdfVO> actionColumn = new TextColumn<PdfVO>() {
		 * 
		 * @Override
		 * 
		 * public String getValue(PdfVO contact) {
		 * 
		 * return contact.getUrl();
		 * 
		 * 
		 * 
		 * 
		 * 
		 * }
		 * 
		 * };
		 */

		// Add the columns.

		// table.addColumn(rowCountColumn, "#");

		Column<PdfVO, Boolean> checkColumn = new Column<PdfVO, Boolean>(

		new CheckboxCell(true, false)) {

			@Override
			public Boolean getValue(PdfVO object) {

				return selectionModel.isSelected(object);

			}

		};

		cellTable.setColumnWidth(checkColumn, 40, Unit.PX);

		cellTable.addColumn(checkColumn, SafeHtmlUtils

		.fromSafeConstant("<br/>"));

		cellTable.addColumn(nameColumn, "Name");

		cellTable.addColumn(dateColumn, "Date");

		cellTable.addColumn(linkColumn, "Action");

	}

	public void populateTable(List<PdfVO> pdfVOs) {

		List<PdfVO> list = dataProvider.getList();

		list.clear();

		for (PdfVO pdfVO : pdfVOs) {

			list.add(pdfVO);

		}

	}

	class BudgetSelectionChangeHandler implements Handler {

		
		public void onSelectionChange(SelectionChangeEvent event) {

			MultiSelectionModel<PdfVO> selectionModel = (MultiSelectionModel<PdfVO>) event

			.getSource();

			Set<PdfVO> selected = selectionModel.getSelectedSet();

		}

	}

	public MultiSelectionModel<PdfVO> getMultiSelectionModel() {
		return (MultiSelectionModel<PdfVO>) selectionModel;
	}
	
	public void addToolBarWidget(Widget widget){
		budgetToolBar.addToolWidget(widget);
	}
}
